package com.coolbi.onsupai.admin.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.onsupai.core.biz.GoodsCategoryBiz;
import com.coolbi.onsupai.core.entity.GoodsCategory;
import com.coolbi.security.web.action.SessionAction;

/**
 * @description:商品类别 Action
 * @author :cgn
 * @createdate :2012-7-1
 */
public class GoodsCategoryAction extends SessionAction {

	private static final long serialVersionUID = 1L;

	private GoodsCategory goodsCategory;
	@Autowired
	private GoodsCategoryBiz goodsCategoryBiz;
	private List<GoodsCategory> goodsCategoryList;
	
	/**
	 * 分页查看所有商品类别
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listByPage() throws Exception {
		pager.setRecordCount(goodsCategoryBiz.count());
		goodsCategoryList = goodsCategoryBiz.listByPage(
				(pager.getCurrPageNo() - 1) * pager.getPageSize(),
				pager.getPageSize());
		logger.debug("goodsCategoryList.size()------>"
				+ goodsCategoryList.size());
		return SUCCESS;
	}

	/**
	 * 获取所有商品类别
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		goodsCategoryList = goodsCategoryBiz.list();
		return SUCCESS;
	}

	/**
	 * 添加商品类别
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			goodsCategoryBiz.save(goodsCategory);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 修改商品类别信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {
		try {
			goodsCategoryBiz.update(goodsCategory);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 删除商品类别
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		try {
			goodsCategoryBiz.delete(goodsCategory.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 根据Id查看商品类别信息
	 * 
	 * @return
	 */
	public String findById() {
		try {
			goodsCategory = goodsCategoryBiz.get(goodsCategory.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 检测商品类别名称是否已存在
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkCategoryName() {
		try {
			boolean flag = false;
			if (null == goodsCategory.getId()) {
				flag = goodsCategoryBiz.checkCategoryName(goodsCategory
						.getCategoryName());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}

	public GoodsCategory getGoodsCategory() {
		return goodsCategory;
	}

	public GoodsCategoryBiz getGoodsCategoryBiz() {
		return goodsCategoryBiz;
	}

	public List<GoodsCategory> getGoodsCategoryList() {
		return goodsCategoryList;
	}

	public void setGoodsCategory(GoodsCategory goodsCategory) {
		this.goodsCategory = goodsCategory;
	}

	public void setGoodsCategoryBiz(GoodsCategoryBiz goodsCategoryBiz) {
		this.goodsCategoryBiz = goodsCategoryBiz;
	}

	public void setGoodsCategoryList(List<GoodsCategory> goodsCategoryList) {
		this.goodsCategoryList = goodsCategoryList;
	}

}
